#pragma once

#include <winsock2.h>
#include <ws2tcpip.h>
//#pragma comment(lib, "ws2_32.lib")

#include <string>
#include <stdexcept>
#include <cstring>

// 地址配置
class InetAddress
{
private:
    struct sockaddr_in addr_ {}; // Windows系统专用的IP地址存储结构
public:
    InetAddress();
    InetAddress(const char *ip, uint16_t port); // ip和端口号

    void setsockaddr(sockaddr_in addr);
    const char* getIp();
    uint16_t getPort();
    const sockaddr_in getAddr() const;

    ~InetAddress();
};

// 网络套接字配置
class Socket
{
private:
    int fd_{-1};
public:
    Socket();
    explicit Socket(int fd);

    // 服务器绑定监听和连接
    void Bind(InetAddress *addr);
    void Listen();
    // 设置套接字为非阻塞模式并检查
    void setNonBlocking();
    bool isNonBlocking();
    int getFd();

    int Accept(InetAddress *addr);
    bool Connect(const InetAddress & serverAddr);

    ~Socket();
};
